<!DOCTYPE html>
<html xmlns:javascrip="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

</head>
<body>

<div style="margin:2% 2% 0 2%">
    <!--<div>-->
        <!--<a  href="/html/resource/resource_list.html" >返回资源列表</a>-->
    <!--</div>-->
    <input type="hidden" id="resource_name">
    <div class="layui-inline mid">
        <button class="layui-btn add" >
            新增数据
        </button>
        <button class="layui-btn layuiadmin-btn-order" id="search" lay-submit="" lay-filter="LAY-app-order-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
        </button>

    </div>
    <table id="demo" lay-filter="xx"></table>
</div>

<script src="/js/include.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/common.js" type="text/javascript" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    var url=window.location.search;
    $("#resource_name").val(parseJson(url).resource_name);
    console.log("资源名",$("#resource_name").val());
    var QueryParam= new Object();
    if(url!=null&&url!="") {
        url = url.substr(1, url.length);
    }
    function parseJson(param)
    {
        var obj={};
        var keyvalue=[];
        var key="",value="";
        var paraString = param.split("&");
        for(var i in paraString)
        {
            keyvalue=paraString[i].split("=");
            key=keyvalue[0];
            value=keyvalue[1];
            obj[key]=value;
        }
        return obj;
    }

    //layui 表格
    layui.use('table', function(){

        //表格数据装载
        var table = layui.table;
        console.log(cols)
        table.render({
            id: 'idTest',
            url:'/data/table/list',
            method:'post',
            where: {resource_name:$("#resource_name").val()},
            //where: {data:JSON.stringify({resource_name:parseJson(url).id})},
            page:true,
            limits:[10,20],
            limit:10,
            elem: '#demo', //指定原始表格元素选择器（推荐id选择器）
            height: 515 ,//容器高度
            weight:1200,
            cols: cols, //设置表头

            response:{
                statusName: 'success', //数据状态的字段名称，默认：code
                statusCode: true ,//成功的状态码，默认：0
                msgName: 'message', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'data' //数据列表的字段名称，默认：data
            }
            //,…… //更多参数参考右侧目录：基本参数选项
        });
        //监听工具条
        table.on('tool(xx)', function tata(obj){

            var data = obj.data,layEvent = obj.event;
            if(layEvent === 'detail'){
                //save(data.id);
                var index = layer.open({
                    type: 2,
                    content: '/html/question/option_info_from.html?id='+data.id,
                    area: ['100%', '100%'],
                    maxmin: false
                });

            }else if(layEvent === 'confirm'){
                //save(data.id);
                var index = layer.open({
                    type: 2,
                    content: '/html/question/option_info_confirm_from.html?id='+data.id,
                    area: ['100%', '100%'],
                    maxmin: false
                });

            } else if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"/layui/table/delete",
                        data:{id:data.id,resource_name:"question_info"},
                        success:function(data){
                            if(data.success){
                                layer.msg("删除成功")
                                obj.del(); //删除对应行（tr）的DOM结构
                            }else {
                                layer.msg(data.message)

                            }
                        }
                    })
//                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    //向服务端发送删除指令
                });
            }
            layer.full(index);
        });
    });


    //编辑后 不刷新 更新选中数据
     function renovation (data) {
         var b=false;
         if(data.id){
             for(var key in data){
                 $(".layui-table-click td[data-field='"+key+"'] div").html(data[key])
             }
         }else {
             query();
         }
         layer.msg("提交成功",{time:5000})
    }
    //查询
    $("#search").click(function () {
        query();
    })
    //新增
    $(".add").click(function () {
        save();
    })
    //导入
    $(".upload").click(function () {

    })
    //保存共用方法
    function save (id) {
        id=(id==undefined)?(""):("?id="+id);

            var layer = layui.layer;

            //layer.msg('hello');

        var index = layer.open({
            type: 2,
            content: '/html/data/data_from.html'+"?id="+""+"&datalist="+encodeURI(JSON.stringify(cols))+"&resource_name="+$("#resource_name").val(),
            area: ['100%', '100%'],
            maxmin: false
        });
        layer.full(index);

    }
    //查询共用方法
    function query () {
        var table = layui.table;
        table.reload('idTest', {
            where: {resource_name:$("#resource_name").val()}
        });
    }
</script>
</body>
</html>